#include "groupchatsessiondetail.h"
#include<QLineEdit>
#include <QPushButton>
#include<QScrollBar>

GroupChatSessionDetail::GroupChatSessionDetail(const QString& groupname,QWidget *parent)
    :QDialog(parent),groupname(groupname)
{
    mainLayout = new QVBoxLayout();
    mainLayout->setContentsMargins(20, 20, 20, 20);
    mainLayout->setSpacing(15);

    this->setWindowTitle("群聊会话详情");
    this->setWindowIcon(QIcon(":/Resources/windowIcon.png"));
    this->setFixedSize(650,700);
    //this->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
    this->setAttribute(Qt::WA_DeleteOnClose);
    this->setStyleSheet(
        "QDialog {"
        "   background: qlineargradient(x1:0, y1:0, x2:1, y2:1,"
        "               stop:0 #1a1a2e, stop:0.5 #16213e, stop:1 #0d0d1a);"
        "   color: #e0e0e0;"
        "   border: 1px solid #333;"
        "}");

    this->setLayout(mainLayout);

    scrllArea = new GroupChatSessionDetailScrollArea();



    groupNameFlag=new QLabel("群组名称");

    groupNameFlag->setStyleSheet(
        "QLabel {"
        "   color: #ffd166;"
        "   font-size: 16px;"
        "   font-weight: bold;"
        "   background: transparent;"
        "   padding: 5px 0px;"
        "}");

    groupNameLayout=new QHBoxLayout();
    groupNameLayout->setContentsMargins(0,0,0,0);
    groupNameLayout->setSpacing(10);

    groupName=new QLabel(this->groupname);
    groupName->setStyleSheet(
        "QLabel {"
        "   color: #e0e0e0;"
        "   font-size: 18px;"
        "   font-weight: bold;"
        "   background: transparent;"
        "   padding: 8px 0px;"
        "}");

    editfinish=new QPushButton();
    editfinish->setIcon(QIcon(":/Resources/submit1.png"));
    editfinish->setFixedSize(35,35);
    editfinish->setIconSize(QSize(25,25));
    editfinish->setStyleSheet(
        "QPushButton {"
        "   background: qlineargradient(x1:0, y1:0, x2:1, y2:0,"
        "               stop:0 #ff6b6b, stop:1 #06d6a0);"
        "   border: none;"
        "   border-radius: 8px;"
        "}"
        "QPushButton:hover {"
        "   background: qlineargradient(x1:0, y1:0, x2:1, y2:0,"
        "               stop:0 #ff8e8e, stop:1 #08e6b0);"
        "}"
        "QPushButton:pressed {"
        "   background: qlineargradient(x1:0, y1:0, x2:1, y2:0,"
        "               stop:0 #ff4b4b, stop:1 #04c690);"
        "}");
    editfinish->hide();

    groupNameLineEdit=new QLineEdit();
    groupNameLineEdit->setText(this->groupname);
    groupNameLineEdit->hide();
    groupNameLineEdit->setStyleSheet(
        "QLineEdit {"
        "   background: rgba(255, 255, 255, 0.1);"
        "   border: 2px solid #06d6a0;"
        "   border-radius: 8px;"
        "   color: white;"
        "   font-size: 16px;"
        "   padding: 5px 10px;"
        "}");

    groupNameModify=new QPushButton();
    groupNameModify->setIcon(QIcon(":/Resources/editbtn2.png"));
    groupNameModify->setFixedSize(35,35);
    groupNameModify->setIconSize(QSize(25,25));
    groupNameModify->setStyleSheet(
        "QPushButton {"
        "   background: qlineargradient(x1:0, y1:0, x2:1, y2:0,"
        "               stop:0 #ff6b6b, stop:1 #06d6a0);"
        "   border: none;"
        "   border-radius: 8px;"
        "}"
        "QPushButton:hover {"
        "   background: qlineargradient(x1:0, y1:0, x2:1, y2:0,"
        "               stop:0 #ff8e8e, stop:1 #08e6b0);"
        "}"
        "QPushButton:pressed {"
        "   background: qlineargradient(x1:0, y1:0, x2:1, y2:0,"
        "               stop:0 #ff4b4b, stop:1 #04c690);"
        "}");

    groupNameLayout->addWidget(groupName);
    groupNameLayout->addWidget(groupNameModify);


    groupWidget=new QWidget();
    groupWidget->setLayout(groupNameLayout);

    quitGruop=new QPushButton("退出群组");
    quitGruop->setFixedHeight(40);
    quitGruop->setStyleSheet(
        "QPushButton {"
        "   background: qlineargradient(x1:0, y1:0, x2:1, y2:0,"
        "               stop:0 #ff6b6b, stop:1 #ef476f);"
        "   border: none;"
        "   border-radius: 8px;"
        "   color: white;"
        "   font-size: 14px;"
        "   font-weight: bold;"
        "}"
        "QPushButton:hover {"
        "   background: qlineargradient(x1:0, y1:0, x2:1, y2:0,"
        "               stop:0 #ff8e8e, stop:1 #f25c7f);"
        "}"
        "QPushButton:pressed {"
        "   background: qlineargradient(x1:0, y1:0, x2:1, y2:0,"
        "               stop:0 #ff4b4b, stop:1 #e63946);"
        "}");

    mainLayout->addWidget(scrllArea);
    mainLayout->addWidget(groupNameFlag);
    mainLayout->addWidget(groupWidget);
    mainLayout->addWidget(quitGruop);

    connect(groupNameModify,&QPushButton::clicked,this,[=](){
        QString text=groupName->text();
        groupName->hide();
        groupNameModify->hide();
        groupNameLayout->removeWidget(groupName);
        groupNameLayout->removeWidget(groupNameModify);


        groupNameLineEdit->setText(text);
        groupNameLineEdit->show();
        editfinish->show();

        groupNameLayout->addWidget(groupNameLineEdit,0,Qt::AlignLeft);
        groupNameLayout->addWidget(editfinish,0,Qt::AlignRight);

    });

    connect(editfinish,&QPushButton::clicked,this,[=](){
        QString text=groupNameLineEdit->text();
        groupNameLineEdit->hide();
        editfinish->hide();
        groupNameLayout->removeWidget(groupNameLineEdit);
        groupNameLayout->removeWidget(editfinish);


        groupName->show();
        groupNameModify->show();

        groupName->setText(text);
        groupNameLayout->addWidget(groupName,0,Qt::AlignLeft);
        groupNameLayout->addWidget(groupNameModify,0,Qt::AlignRight);

    });

}

void GroupChatSessionDetail::addIemToScrollArea(const QIcon &icon, const QString &text)
{
    QPushButtonAndQLael*item=new QPushButtonAndQLael(icon,text);
    this->scrllArea->addItem(item);
}

GroupChatSessionDetailScrollArea::GroupChatSessionDetailScrollArea(QWidget *parent)
    :QScrollArea(parent)
{


    layout = new QGridLayout();
    layout->setContentsMargins(15, 15, 15, 15);
    layout->setHorizontalSpacing(30);
    layout->setVerticalSpacing(5);



    contain=new QWidget();
    contain->setLayout(layout);
    contain->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

    this->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    this->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    this->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
    this->setWidget(contain);
    this->setWidgetResizable(true);
    // 设置滚动区域背景
    this->setStyleSheet(
        "GroupChatSessionDetailScrollArea {"
        "   background: qlineargradient(x1:0, y1:0, x2:1, y2:0,"
        "               stop:0 rgba(45, 45, 65, 0.7), stop:1 rgba(55, 55, 75, 0.5));"
        "   border: 1px solid rgba(255, 255, 255, 0.1);"
        "   border-radius: 12px;"
        "}"
        "QScrollArea QWidget {"
        "   background: transparent;"
        "}");

    // 设置容器背景
    contain->setStyleSheet(
        "QWidget {"
        "   background: transparent;"
        "   border: none;"
        "}");
    // 美化滚动条
    this->verticalScrollBar()->setStyleSheet(
        "QScrollBar:vertical {"
        "   width: 10px;"
        "   background: rgba(255, 255, 255, 0.05);"
        "   border-radius: 5px;"
        "}"
        "QScrollBar::handle:vertical {"
        "   background: qlineargradient(x1:0, y1:0, x2:1, y2:0,"
        "               stop:0 #ff6b6b, stop:1 #06d6a0);"
        "   border-radius: 5px;"
        "   min-height: 30px;"
        "}"
        "QScrollBar::handle:vertical:hover {"
        "   background: qlineargradient(x1:0, y1:0, x2:1, y2:0,"
        "               stop:0 #ff8e8e, stop:1 #08e6b0);"
        "}"
        "QScrollBar::handle:vertical:pressed {"
        "   background: qlineargradient(x1:0, y1:0, x2:1, y2:0,"
        "               stop:0 #ff4b4b, stop:1 #04c690);"
        "}");
    this->horizontalScrollBar()->setStyleSheet("QScrollBar:horizontal { height: 0px; }");

    addwidget=new QPushButtonAndQLael(QIcon(":/Resources/plus.png"),"添加");
    layout->addWidget(addwidget,0,0,1,1,Qt::AlignTop|Qt::AlignCenter);
}

void GroupChatSessionDetailScrollArea::addItem(QPushButtonAndQLael *item)
{
    if(col>=4)
    {

        col=0;
        row++;
    }
    layout->addWidget(item,row,col++,1,1,Qt::AlignTop|Qt::AlignCenter|Qt::AlignLeft);

}
